﻿using System;
using System.IO;
using System.Text;
using EmbeddedSharp.Scripting;
using WeifenLuo.WinFormsUI.Docking;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Zove.Desktop.Tools {
    public partial class InteractiveMode : DockContent, IDesktopView {

        private ScriptingObject vm = null;

        public void RunInBackground()
        {
        }

        public InteractiveMode() {
            InitializeComponent();

            TextBoxWriter tbw = new TextBoxWriter(this.tecOutput);
            Console.SetOut(tbw);

            this.vm = new ScriptingObject(new ScriptingContext());

            this.tecInput.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            this.tecInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
            this.tecInput.AutoCompleteCustomSource
                = new AutoCompleteStringCollection();
            this.tecOutput.DocumentSelectionMode = DigitalRune.Windows.TextEditor.Properties.DocumentSelectionMode.Additive;
            this.tecOutput.Document.DocumentChanged 
                += new EventHandler<
                    DigitalRune.Windows.TextEditor.Document.DocumentEventArgs>(
                    Document_DocumentChanged);
            this.tecOutput.Text = "Zove Desktop for Microsoft Windows\n@ 2009 Zove System\n";
        }

        public void Document_DocumentChanged(object sender, EventArgs e) {
            
        }

        private void btnCommit_Click(object sender, EventArgs e) {
            this.Run();
        }

        private List<string> history = new List<string>();

        #region IDesktopForm Members

        /// <summary>
        /// 
        /// </summary>
        public void Validate()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public void Stop()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public void Run() {
            string input = this.tecInput.Text.Trim();
            if (string.IsNullOrEmpty(input)) {
                return;
            }

            if (input == "%") {

            }

            this.tecInput.AutoCompleteCustomSource.Add(input);
            try {
                string output = Convert.ToString(
                    this.vm.Process(input));
                output = string.Format("> {0}\n< {1}\n", input, output);
                this.tecOutput.Text = this.tecOutput.Text + output;
                this.tecInput.SelectAll();
            } catch (Exception e) {
                MessageBox.Show(e.Message);
            }
        }

        public DigitalRune.Windows.TextEditor.TextEditorControl GetTextEditorControl() {
            return this.tecOutput;
        }

        public string GetFileName() {
            throw new NotImplementedException();
        }

        public string GetFileExtension() {
            throw new NotImplementedException();
        }

        public void Save(string fileName) {
            StringBuilder b = new StringBuilder();

            foreach (string stmt in ((ScriptingContext)this.vm.Context).InputHistory) {
                b.Append(stmt);
            }

            File.WriteAllText(fileName, b.ToString());
        }

        public string GetFileFilter() {
            throw new NotImplementedException();
        }

        public string GetColourScheme() {
            return "Zoom";
        }

        #endregion

        #region IDesktopForm Members


        public void Debug() {
            throw new NotImplementedException();
        }

        public void Resume() {
            throw new NotImplementedException();
        }

        public void Restart() {
            throw new NotImplementedException();
        }

        public double Profile() {
            throw new NotImplementedException();
        }

        #endregion
    }
}
